package cn.iocoder.yudao.module.system.service.camera;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.camera.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.camera.CameraDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.system.convert.camera.CameraConvert;
import cn.iocoder.yudao.module.system.dal.mysql.camera.CameraMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 设备列表 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CameraServiceImpl implements CameraService {

    @Resource
    private CameraMapper cameraMapper;

    @Override
    public Long createCamera(CameraCreateReqVO createReqVO) {
        // 插入
        CameraDO camera = CameraConvert.INSTANCE.convert(createReqVO);
        cameraMapper.insert(camera);
        // 返回
        return camera.getId();
    }

    @Override
    public void updateCamera(CameraUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCameraExists(updateReqVO.getId());
        // 更新
        CameraDO updateObj = CameraConvert.INSTANCE.convert(updateReqVO);
        cameraMapper.updateById(updateObj);
    }

    @Override
    public void deleteCamera(Long id) {
        // 校验存在
        this.validateCameraExists(id);
        // 删除
        cameraMapper.deleteById(id);
    }

    private void validateCameraExists(Long id) {
        if (cameraMapper.selectById(id) == null) {
            throw exception(CAMERA_NOT_EXISTS);
        }
    }

    @Override
    public CameraDO getCamera(Long id) {
        return cameraMapper.selectById(id);
    }

    @Override
    public List<CameraDO> getCameraList(Collection<Long> ids) {
        return cameraMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CameraDO> getCameraPage(CameraPageReqVO pageReqVO) {
        return cameraMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CameraDO> getCameraList(CameraExportReqVO exportReqVO) {
        return cameraMapper.selectList(exportReqVO);
    }

}
